package com.ruoyi.finance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.finance.vo.VoucherDetailVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.finance.domain.Voucher;
import com.ruoyi.finance.service.IVoucherService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 凭证列表Controller
 *
 * @author ruoyi
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/finance/voucher")
public class VoucherController extends BaseController
{
    @Autowired
    private IVoucherService voucherService;

    /**
     * 查询凭证列表列表
     */
    @PreAuthorize("@ss.hasPermi('finance:voucher:list')")
    @GetMapping("/list")
    public TableDataInfo list(Voucher voucher)
    {
        startPage();
        List<Voucher> list = voucherService.selectVoucherList(voucher);
        return getDataTable(list);
    }

    /**
     * 导出凭证列表列表
     */
    @PreAuthorize("@ss.hasPermi('finance:voucher:export')")
    @Log(title = "凭证列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Voucher voucher)
    {
        List<Voucher> list = voucherService.selectVoucherList(voucher);
        ExcelUtil<Voucher> util = new ExcelUtil<Voucher>(Voucher.class);
        util.exportExcel(response, list, "凭证列表数据");
    }

    /**
     * 获取凭证列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:voucher:query')")
    @GetMapping(value = "/{voucherId}")
    public AjaxResult getInfo(@PathVariable("voucherId") Long voucherId)
    {
        return success(voucherService.selectVoucherByVoucherId(voucherId));
    }

    /**
     * 新增凭证列表
     */
    @PreAuthorize("@ss.hasPermi('finance:voucher:add')")
    @Log(title = "凭证列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Voucher voucher)
    {
        return toAjax(voucherService.insertVoucher(voucher));
    }


    //TODO:凭证列表添加
    @PostMapping("/toadd")
    public AjaxResult toadd(@RequestBody VoucherDetailVo voucherDetailVo){
        return AjaxResult.success(voucherService.toadd(voucherDetailVo));
    }

    /**
     * 修改凭证列表
     */
    @PreAuthorize("@ss.hasPermi('finance:voucher:edit')")
    @Log(title = "凭证列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Voucher voucher)
    {
        return toAjax(voucherService.updateVoucher(voucher));
    }

    /**
     * 删除凭证列表
     */
    @PreAuthorize("@ss.hasPermi('finance:voucher:remove')")
    @Log(title = "凭证列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{voucherIds}")
    public AjaxResult remove(@PathVariable Long[] voucherIds)
    {
        return toAjax(voucherService.deleteVoucherByVoucherIds(voucherIds));
    }
}
